<body class="gr">

<div class="navBar">
	<a class="left" href="wap.php?app=member&act=index"><i class="return"></i></a>
	<div class="titleView">我的订单</div>
</div>

<div class="member-order main-padding-top">
	<div class="pullRefresh overflow-scroll" status="{if isset($status)}{$status}{/if}">
		<header class="ge-bottom ge-light">
			<ul class="switchView">
				<li {if !isset($status)}class="this"{/if}><a href="wap.php?app=member&act=order">全部</a></li>
				<li {if isset($status) && $status==0}class="this"{/if}><a href="wap.php?app=member&act=order&status=0">待付款</a></li>
				<li {if isset($status) && $status==1}class="this"{/if}><a href="wap.php?app=member&act=order&status=1">待发货</a></li>
				<li {if isset($status) && $status==2}class="this"{/if}><a href="wap.php?app=member&act=order&status=2">待收货</a></li>
				<li {if isset($status) && $status==3}class="this"{/if}><a href="wap.php?app=member&act=order&status=3">待评价</a></li>
				<li {if isset($status) && $status==4}class="this"{/if}><a href="wap.php?app=member&act=order&status=4">完成</a></li>
				<li {if isset($status) && $status=='-1'}class="this"{/if}><a href="wap.php?app=member&act=order&status=-1">取消</a></li>
				<li {if isset($status) && $status=='-2,-3'}class="this"{/if}><a href="wap.php?app=member&act=order&status=-2,-3">退货/退款</a></li>
			</ul>
		</header>
		
		{if count($data)>0}
		<ul class="list tableView tableView-noLine">
			{foreach from=$data name=g item=g}
			<li>
				<div class="view">
					<a href="?app=member&act=order_detail&id={$g->id}">
						<div class="status ge-bottom ge-light status{$g->status}"><font>订单号：{$g->order_sn}</font><div>{$g->status}</div>{if $g->status>0 && $g->ask_refund_time>0}<span>(退货退款中)</span>{/if}</div>
						<ul>
							{foreach from=$g->goods name=p item=p}
							<li>
								<div class="gview">
									<div class="right"><span class="price">￥{number_format($p->price,2)}</span><span>× {$p->quantity}</span></div>
									<div class="pic" style="background-image:url({$p->goods_pic});"></div>
									<div class="name">{$p->goods_name}</div>
									<div class="spec">规格：{$p->spec}</div>
								</div>
							</li>
							{/foreach}
						</ul>
					</a>
					<div class="total" mid="{$g->id}">
						{if $g->status==0}
						<a class="pay" href="javascript:void(0)" pay_method="{$g->pay_method}"><span>立即支付</span></a>
						<form class="pay_form" action="wap.php?app=member&act=order_pay" method="post">
						<input type="hidden" name="re_order_sn" value="" />
						<input type="hidden" name="order_sn" value="{$g->order_sn}" />
						<input type="hidden" name="price" value="{$g->total_price}" />
						<input type="hidden" name="body" value="商富商城-订单" />
						</form>
						{/if}
						{if $g->status==1}
						{if $g->ask_refund_time==0}
						<a class="refund" href="javascript:void(0)" total_price="{$g->total_price}" status="{$g->status}"><span>我要退款</span></a>
						{/if}
						{/if}
						{if $g->status==2}
						<a class="ok" href="javascript:void(0)"><span>确认收货</span></a>
						{/if}
						总计：<strong>￥{number_format($g->total_price,2)}</strong> ({if $g->shipping_price>0}含运费￥{number_format($g->shipping_price,2)}{else}免邮{/if})
					</div>
				</div>
			</li>
			{/foreach}
		</ul>
		{else}
		<div class="norecord">当前没有任何订单</div>
		{/if}
	</div>
</div>

<script>
var offset = $('.pullRefresh .list > li').length;
function createHtml(g){
	var html = '<li>\
		<div class="view">\
			<a href="?app=member&act=order_detail&id='+g.id+'">\
				<div class="status ge-bottom ge-light status'+g.status+'"><font>订单号：'+g.order_sn+'</font><div>'+statusName(g.status)+'</div>'+((g.status>0 && g.ask_refund_time>0)?'<span>(退货退款中)</span>':'')+'</div>\
				<ul>';
				for(var i=0; i<g.goods.length; i++){
				html += '<li>\
						<div class="gview">\
							<div class="right"><span class="price">￥'+g.goods[i].price.numberFormat(2)+'</span><span>× '+g.goods[i].quantity+'</span></div>\
							<div class="pic" style="background-image:url('+g.goods[i].goods_pic+');"></div>\
							<div class="name">'+g.goods[i].goods_name+'</div>\
							<div class="spec">规格：'+g.goods[i].spec+'</div>\
						</div>\
					</li>';
				}
				html += '</ul>\
			</a>\
			<div class="total" mid="'+g.id+'">';
				if(g.status==0){
				html += '<a class="pay" href="javascript:void(0)" pay_method="'+g.pay_method+'"><span>立即支付</span></a>\
				<form class="pay_form" action="wap.php?app=member&act=order_pay" method="post">\
				<input type="hidden" name="re_order_sn" value="" />\
				<input type="hidden" name="order_sn" value="'+g.order_sn+'" />\
				<input type="hidden" name="price" value="'+g.total_price+'" />\
				<input type="hidden" name="body" value="商富商城-订单" />\
				</form>';
				}
				if(g.status==1){
					if(g.ask_refund_time==0)html += '<a class="refund" href="javascript:void(0)" total_price="'+g.total_price+'" status="'+g.status+'"><span>我要退款</span></a>';
				}
				if(g.status==2){
					html += '<a class="ok" href="javascript:void(0)"><span>确认收货</span></a>';
				}
				html += '总计：<strong>￥'+g.total_price.numberFormat(2)+'</strong> <span>('+(g.shipping_price>0?'含运费￥'+g.shipping_price.numberFormat(2):'免邮')+')</span>';
			html += '</div>\
		</div>\
	</li>';
	offset++;
	return html;
}
function statusName(status){
	var name = '';
	switch(status*1){
		case -3:name = '已退货';break;
		case -2:name = '已退款';break;
		case -1:name = '取消';break;
		case 0:name = '待付款';break;
		case 1:name = '待发货';break;
		case 2:name = '待收货';break;
		case 3:
		case 4:name = '完成';break;
	}
	return name;
}
function setInfo(){
	$('.gview .pic').loadbackground();
	$('.tableView .total strong').priceFont('bigPrice');
	$('.gview .right span.price').priceFont('bigPrice');
}
$(function(){
	$('.member-order').height($.window().height-$('.member-order').padding().top);
	$('.switchView').switchView({ column:'column', index:$('.switchView .this').index() });
	$('.tableView .status div').html(function(i, html){ return statusName(html) });
	setInfo();
	$(document.body).on('click', 'a.pay', function(){
		if($.browser.wechat && $(this).attr('pay_method')!='wxpay' && $(this).attr('pay_method')!='wxpay_h5'){
			alert('在微信上只能使用微信支付\n该订单下单时使用了支付宝方式');
			return false;
		}
		$(this).parent().find('.pay_form').submit();
	});
	$(document.body).on('click', 'a.refund', function(){
		location.href = 'wap.php?tpl=member.order_refund&order_id='+$(this).parent().attr('mid')+'&total_price='+$(this).attr('total_price')+'&status='+$(this).attr('status');
	});
	$(document.body).on('click', 'a.ok', function(){
		if(!confirm('请收到货后，再确认收货！\n否则您可能钱货两空'))return false;
		$.postJSON('api.php?app=member&act=order_shouhuo', { id:$(this).parent().attr('mid') }, function(json){
			if(json.error!=0){ $.overloadError(json.msg);return }
			location.href = location.href;
		});
	});
	$('.pullRefresh').pullRefresh({
		header : true,
		footer : true,
		footerNoMoreText : '- END -',
		refresh : function(fn){
			var _this = this;
			offset = 0;
			$.getJSON('api.php?app=member&act=order&status='+_this.attr('status'), function(json){
				if(json.error!=0){ $.overloadError(json.msg);return }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.tableView').html(html);
				setInfo();
				fn();
			});
		},
		load : function(fn){
			var _this = this;
			$.getJSON('api.php?app=member&act=order&status='+_this.attr('status'), { offset:offset }, function(json){
				if(json.error!=0){ $.overloadError(json.msg);return }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.tableView').append(html);
				setInfo();
				fn();
			});
		}
	});
});
</script>
